package com.example.hxpc.guidancemanagement.tools;

import android.content.Context;
import android.content.SharedPreferences;


import java.io.File;

/**
 * Created by tao on 2017/12/27.
 */

public class SharePreferenceTools {
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static final String FILE_NAME = "guidance_management";
    private static SharePreferenceTools preferenceTools;

    /**
     * 获取本类对象实例
     * @param context
     * @return
     */
    public synchronized static SharePreferenceTools getInstance(Context context){
        if (preferenceTools == null){
            preferenceTools = new SharePreferenceTools();
            sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
            editor = sp.edit();
        }
        return preferenceTools;
    }

//    public UserInfo saveUserInfo(UserInfo user){
//        editor.put.commit();
//    }


    public void setString(String key ,String value){
        editor.putString(key,value).commit();
    }
    public String getString(String key){
        return sp.getString(key,"");
    }

    public void setBoolean(String key ,boolean value ){
        editor.putBoolean(key,value).commit();
    }
    public boolean getBoolean(String key){
        return sp.getBoolean(key ,false);
    }

    public void setInt(String key ,int value){
        editor.putInt(key,value).commit();
    }
    public int getInt(String key){
        return sp.getInt(key,0);
    }

    public void cleanSharedPreference(Context context){
        File directory = new File("/data/data/"+context.getPackageName()+"/shared_prefs");
        LogoutT.logout("清除缓存");
        if (directory != null&&directory.exists()&&directory.isDirectory()){
            for(File item : directory.listFiles()){
                LogoutT.logout("不为空!");
                item.delete();
            }
        }
    }

    public boolean cleanData(){
        editor.clear();
        if (editor.commit())
            return true;
        else
            return false;
    }

}
